import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-06-05
 * Time: 22:14
 */
class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}

public class Test {
    public Node copyRandomList(Node head) {
        Map<Node, Node> map = new HashMap<>();
        //遍历链表
        Node cur = head;
        while (cur != null) {
            Node NewNode = new Node(cur.val);
            map.put(cur, NewNode);//key老结点,value新结点
            cur = cur.next;
        }
        //代码到这,就已经构建好了map
        //重新遍历一遍
        cur = head;
        while (cur != null) {
//            map.get(cur)表示新的结点,
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }


}
